package org.example.warehouse.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author 小杰_cc
 * @version 1.0
 */
@Configuration
public class MyMqConfig {

    // 交换机
    @Bean
    public TopicExchange stockEventExchange(){
        return ExchangeBuilder.topicExchange("stock-event-exchange").durable(true).build();
    }

    // 普通队列
    @Bean
    public Queue stockReleaseQueue(){
        return QueueBuilder.durable("stock.release.stock.queue").build();
    }

    // 延迟队列
    @Bean
    public Queue stockDelayQueue(){
        return QueueBuilder.durable("stock.delay.queue")
                .ttl(1200000)    // 20分钟
                .deadLetterExchange("stock-event-exchange")
                .deadLetterRoutingKey("stock.release")
                .build();
    }

    // 绑定关系
    @Bean
    public Binding stockReleaseBinding(){
        return BindingBuilder.bind(stockReleaseQueue()).to(stockEventExchange()).with("stock.release.#");
    }

    // 绑定关系
    @Bean
    public Binding stockLockedBinding(){
        return BindingBuilder.bind(stockDelayQueue()).to(stockEventExchange()).with("stock.locked");
    }
}
